<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\express\admin;

use app\admin\controller\Admin;
use app\express\model\Express as express;
use app\common\builder\ZBuilder;


class Index extends Admin
{
    function _initialize()
    {
        parent::_initialize();
        $this->expressModel=new express();
        $_SESSION['sys_projectid'] = 66;

    }

    /**
     * @return mixed分类插件后台入口
     */
    function index()
    {
        $map = $this->getMap();
        $map['projectid'] = $_SESSION['sys_projectid'];
        $map['isdel'] = 2;
        $data_list=$this->expressModel->where($map)->order('number_orders asc')->paginate();
        return ZBuilder::make('table')
            ->setPageTitle('配送方式管理')// 设置页面标题
            ->setTableName('express_express')// 设置数据表名
            ->setSearch(['id' => 'ID', 'dispatchname' => '配送方式名称'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['__INDEX__', '序号'],
                ['id', 'ID'],
                ['dispatchname', '配送方式名称'],
                ['dispatchtype', '配送类型', 'status', '', [1 => '商家配送', 2 => '自提']],
                ['createtime', '创建时间', ''],
                ['status', '状态', 'status', '',[1 => '显示', 2 => '不显示']],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染页面
    }

    function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
           $data['areas']=serialize($data['area']);
            $data['projectid'] = $_SESSION['sys_projectid'];
            $data['createtime'] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
            $result = $this->expressModel->addExpress($data);
            if ($result) {
                $this->success('添加成功!', url('index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
        //return ZBuilder::make('form')->addImage('post[icon]', '封面')->fetch('/add');
    }


    /**
     * 编辑配送方式
     * @param $id  分类id
     */
    function edit($id = null)
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data, 'Form');
            if (true !== $result) $this->error($result);
            $data['areas']=serialize($data['areas']);
            $where['projectid'] = $_SESSION['sys_projectid'];
            $where['id'] = $id;

            if ($this->expressModel->update($data, $where)) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
        if ($id > 0) {
            $express = express::get($id)->toArray();
            $ares=empty($express['areas'])?array():unserialize($express['areas']);
            $count=count($ares);
            $this->assign('express',$express);
            $this->assign('areasnum',$count);
            $this->assign('areas',$ares);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
    }

    /**
     * 删除配送方式
     * @param $ids 分类id
     */

    public function delete($record = [])
    {
        $param = $this->request->param();
        $ids = $param['ids'];
        $where['id'] = $ids;
        if (is_array($ids)) {
            $where['id'] = array('in', $ids);
        }
        $where['projectid'] = $_SESSION['sys_projectid'];
        $data['isdel'] = 1;
        if ($this->expressModel->update($data, $where)) {
            //action_log('user_add', 'admin_user', $user['id'], UID);
            $this->success('修改成功', url('index'));
        } else {
            $this->error('修改失败');
        }
    }


}
